<?php
session_start();
$user = $_SESSION['user'];
require_once "../php/config/database.php";
require_once "../php/user/userDb.php";
$UserDb=new UserDb();
$data=$UserDb->selectUserAdmin($user);
$group=$data[0]['class'];
$id=$data[0]['id'];
//try {
//    $result = $data;
//    if ($result) {
//
//    }
//    else {
//
//        echo "<script>alert('您还没有该团队的成员哦！很抱歉暂时不能发布任务');</script>";
//        echo "<script> window.location.href='add_user.php'; </script>";
//
//    }
//}
//catch (Exception $e) {
//    echo $e->getMessage();
//}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="shortcut icon" href="ico/logo.ico" type="image/x-icon" />
    <title>协同办公-任务</title>
    <link rel="stylesheet" href="../static/css/bootstrap.css">
    <link rel="stylesheet" href="../static/js/layui/css/layui.css">
    <link rel="stylesheet" href="../static/css/font.css">
    <link rel="stylesheet" href="../static/css/main.css">
    <style>
        #title-form{
            width: 200px;
        }
    </style>
</head>
<body>
<div id="logo"><div id="logo-img"><img src="../static/imaegs/logo/logo2.png"></div>
<div id="logo-right">
    <h5 ><i class="layui-icon" style="font-size: 18px; color: orange">&#xe645;</i>&nbsp;最新公告：<span id="notice">暂无公告</span></h5>
</div>
</div>
<div id="tiao">
    <div id="left"><a href="index.php"><i class="layui-icon" style="font-size: 20px; color: #1E9FFF;">&#xe68e;</i>&nbsp;首页</a>
    <a style="border-bottom: 2px blue solid"> <i class="layui-icon" style="font-size: 20px; color: #1E9FFF;">&#xe6b2;</i>&nbsp;任务</a>
        <a href="notice/notice.php"> <i class="layui-icon" style="font-size: 20px; color: #1E9FFF;">&#xe637;</i>&nbsp;公告栏</a>
        <a href="file.php"><i class="layui-icon" style="font-size: 20px; color: #1E9FFF;">&#xe621;</i>&nbsp;文件管理</a>
        <a href="add_user.php"><i class="layui-icon" style="font-size: 20px; color: #1E9FFF;">&#xe613;</i>&nbsp;用户管理</a></div>
    <div id="right"><a style="color:grey" href="index.php">您好！&nbsp;</a><i class="layui-icon" style="font-size: 20px; color: #1E9FFF;">&#xe66f;</i><input id="user" name="user" readonly="readonly" value="<?php echo $user ?>"><a onclick="out()">退出</a></div>
</div>
<hr class="layui-bg-gray">
<div id="from_block">
    <legend><i class="iconfont" style="color: #0b3b8c">&#xe6b2;写任务：</i></legend>
    <div class="layui-form-item">
        <label class="layui-form-label login-form" id="title-form"><i class="iconfont" style="color: #0b3b8c">&#xe6b8;</i>任务详情：</label>
        <div class="layui-input-block">
            <textarea class="layui-textarea" maxlength="500" id="task_content" name="task_content" placeholder="请输入内容,内容字数不超过500字"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label login-form" id="title-form">设置任务完成时间：</label>
        <div class="layui-input-inline">
            <input name="date" class="layui-input" id="date" type="text" placeholder="2018年6月1日" autocomplete="off" lay-verify="date">

        </div>
    </div>



    <input type="text"  name="user" id="user" style="display: none"  value="<?php echo $user; ?>">
    <legend><i class="iconfont" style="color: #0b3b8c">&#xe6b2;分配任务：</i></legend>
    <div class="layui-form-item">
        <label class="layui-form-label login-form" id="title-form"><i class="iconfont" style="color: #0b3b8c">&#xe6b8;</i>任务：</label>
        <div class="layui-input-block">
            <textarea class="layui-textarea" maxlength="300" id="task_a" name="task_a" placeholder="请输入内容,内容字数不超过300字"></textarea>
        </div>
    </div>
    <form class="layui-form" action="" style="width: 400px">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;font-size: 9px">分配给：</label>
            <div class="layui-input-block">
                <select name="selectOne"  lay-filter="aihao" id="selectOne">
                    <option value="" selected="" id="option">自定义分配</option>
                    <option value="" selected="" id="option">不指定分配</option>
                </select>
            </div>
        </div>
    </form>


    <input type="text" id="file-url" name="file-url" style="display: none">
    <label class="layui-form-label login-form" id="title-form"><i class="iconfont" style="color: #0b3b8c">&#xe6b8;</i>备注：</label>
    <div class="layui-input-block">
        <textarea class="layui-textarea" maxlength="200" id="bei-zhu" name="bei-zhu" placeholder="请输入内容,内容字数不超过300字"></textarea>
        <br>
        <button class="layui-btn" lay-submit lay-filter="login" style="float: right;margin-left: 20px" onclick="email()" type="button">邮件通知</button>
        <button class="layui-btn layui-btn-warm " lay-submit lay-filter="login" style="float: right" onclick="release()" type="button">确认发布</button>
        <br><hr>温馨提示：邮件发送失败，请正确配置自己的邮箱哦<br>
    </div>

    <legend><i class="iconfont" style="color: #0b3b8c">&#xe6b2;附件上传：</i></legend>
    <div class="layui-form-item">
        <label class="layui-form-label login-form" id="title-form"><i class="iconfont" style="color: #0b3b8c">&#xe6b8;</i>选择文件：</label>
        <div class="layui-input-block">
            <form action="" enctype="multipart/form-data" method="">
                <a class="opts_btn" href="JavaScript:void(0)">
                    <input  name="myFile" class="oFile" id="oFile"  onchange="FileChangeFn(event)" type="file">
                </a>
                <label id="file_size"></label><br><br>
                <div id="file_box"></div>
                <div class="speed_box">
                    <div id="speed">0%</div>
                </div>
                <label id="file_type"></label>
                <button class="layui-btn" onclick="UploadFileFn()" type="button">开始上传文件</button>
                <div class="clear"></div> <input style="display: none" class="hui-input" id="url" name="url"  type="text">
                <br><hr>温馨提示：附件允许的大小为20m<br>
            </form>
        </div>
    </div>


    <legend><i class="iconfont" style="color: #0b3b8c">&#xe60a;任务列表：</i></legend>
    <hr class="layui-bg-blue">
    <div class="layui-row layui-col-space15" >
        <div class="layui-col-md12" id="uul">

        </div>
    </div>













</div>
<hr>


<script src="../static/js/jquery.min.js"></script>
<script src="../static/js/layui/layui.all.js"></script>
<script>
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });





    });

     $user=$('#user').val();
     if($user===""){
        layer.msg("您还没有登陆哦！");
        window.location.href="login.html";

    }
    var uploadUrl = '../php/admin/upload.php?id=<?php echo $id;?>';
    //文件选择完毕时
    function FileChangeFn(event) {
        $('.opst_txt').text('重新选择文件');
        $('.send_btn').show();
        var event = event || window.event,
            dom = '',
            ofile = $("#oFile").get(0).files[0],
            otype = ofile.type || '获取失败',
            osize = ofile.size / 1054000,
            ourl = window.URL.createObjectURL(ofile); //文件临时地址
        $('#file_type').text("选择上传文件类型：" + otype);
        $('#file_size').text("选择上传文件大小，共" + osize.toFixed(2) + "MB。");

        console.log("文件类型：" + otype); //文件类型
        console.log("文件大小：" + osize); //文件大小

        if ('video/mp4' == otype || 'video/avi' == otype || 'video/x-msvideo' == otype) {
            layer.msg("请上传压缩文件/或者图片");
        }
        if ('audio/mp3' == otype || 'audio/wav' == otype  || 'audio/x-m4a' == otype) {
            layer.msg("请上传压缩文件/或者图片");
        }
        if ('image/jpeg' == otype || 'image/png' == otype || 'image/gif' == otype) {
            dom = '<img id="photo" width="100px"  alt="我是image图片文件" src=' + ourl + ' title="" />';
        }
        if ('zip' == otype) {
            dom = '<img id="photo" width="100px"  alt="我是压缩文件" src=' + ourl + ' title="" />';
        }
        $('#file_box').html(dom);
    };

    //侦查附件上传情况 ,这个方法大概0.05-0.1秒执行一次
    function OnProgRess(event) {
        var event = event || window.event;
        //console.log(event);  //事件对象
        console.log("已经上传：" + event.loaded); //已经上传大小情况(已上传大小，上传完毕后就 等于 附件总大小)
        //console.log(event.total);  //附件总大小(固定不变)
        var loaded = Math.floor(100 * (event.loaded / event.total)); //已经上传的百分比
        $("#speed").html(loaded + "%").css("width", loaded + "%");
    };

    //开始上传文件
    function UploadFileFn() {


        var file=document.getElementById("oFile");
        fileV=file.value;
        if(fileV===""){
            layer.msg("您还没有选择任何文件哦")
        }
        else {

            $('.speed_box').show();
            var oFile = $("#oFile").get(0).files[0], //input file标签
                formData = new FormData(); //创建FormData对象
            xhr = $.ajaxSettings.xhr(); //创建并返回XMLHttpRequest对象的回调函数(jQuery中$.ajax中的方法)
            formData.append("myFile", oFile); //将上传name属性名(注意：一定要和 file元素中的name名相同)，和file元素追加到FormData对象中去

            $.ajax({
                type: "POST",
                url: uploadUrl, // 后端服务器上传地址
                data: formData, // formData数据
                cache: false, // 是否缓存
                async: true, // 是否异步执行
                processData: false, // 是否处理发送的数据  (必须false才会避开jQuery对 formdata 的默认处理)
                contentType: false, // 是否设置Content-Type请求头
                dataType: 'json',
                xhr: function () {
                    if (OnProgRess && xhr.upload) {
                        xhr.upload.addEventListener("progress", OnProgRess, false);
                        return xhr;
                    }
                },
                success: function (dat) {

                    $("#speed").html("上传成功");
                    var url=document.getElementById("file-url");
                    url.value=dat.src;
                },
                error: function (dat) {
                    $("#speed").html("上传失败");
                }
            });
        }
    }

    function email() {
        var task_content =document.getElementById("task_content");
        if (task_content.value === '') {
            layer.msg ('你的任务还未填写');
            return false;


        }
        else {
            var user=document.getElementById("user"),
                task_a=document.getElementById("task_a"),
                date=document.getElementById("date"),
                task_a=document.getElementById("task_a"),
                UserA=document.getElementById("selectOne"),
                bei=document.getElementById("bei-zhu"),
                url=document.getElementById("file-url");
            $user=user.value;
            $bei=bei.value;
            $date=date.value;
            $userA=UserA.value;
            $task_a=task_a.value;
            $task_a=task_a.value;
            $url=url.value;
            $task_content = task_content.value;
            $.ajax({
                type: 'post',
                cache: 'false',
                url: '../php/phpmail/sendmail.php',
                data: {
                    "task_content": $task_content,
                    "user":$user,
                    "task_a":$task_a,
                    "date":$date,
                    "taske_a":$task_a,
                    "userA":$userA,
                    "remarks":$bei,

                },
                dataType: 'Json',
                success: function (dat) {
                    layer.msg(dat.message);
                },
                error: function (dat) {
                    layer.msg("发送邮件失败，请检查您的网络");
                }


            })


        }
    }

    function release() {
        var task_content =document.getElementById("task_content");
        if (task_content.value === '') {
            layer.msg ('你的任务还未填写');
            return false;


        }
        else {
            var user=document.getElementById("user"),
            task_a=document.getElementById("task_a"),
            date=document.getElementById("date"),
            task_a=document.getElementById("task_a"),
                UserA=document.getElementById("selectOne"),
            bei=document.getElementById("bei-zhu"),
            url=document.getElementById("file-url");
            $user=user.value;
            $bei=bei.value;
            $date=date.value;
            $userA=UserA.value;
            $task_a=task_a.value;
            $task_a=task_a.value;
            $url=url.value;
            $task_content = task_content.value;
            $.ajax({
                type: 'post',
                cache: 'false',
                url: '../php/admin/add_task.php',
                data: {
                    "task_content": $task_content,
                    "user":$user,
                    "task_a":$task_a,
                    "date":$date,
                    "taske_a":$task_a,
                    "userA":$userA,
                    "remarks":$bei,
                    "url":$url,
                },
                dataType: 'Json',
                success: function (dat) {
                    layer.msg(dat.message);


                },
                error: function (dat) {
                    layer.msg("发布失败");
                }


            })


        }







    }

    $.ajax({
        type: 'post',
        cache: false,
        url: '../php/admin/select_notice.php',
        data: {
            "user":$user,
        },
        dataType: 'json',
        success: function (data) {
            $dat = data.data;
            $coun = data.count;
            $notice=$dat[0].content;
            var not=document.getElementById("notice");
            not.innerHTML=$notice;


        },
        error: function (data) {

        },
    });

    $.ajax({
        type: 'post',
        cache: false,
        url: '../php/user/select_user_class.php?class=<?php echo $group ?>',
        data: {
        },
        dataType: 'json',
        success: function (data) {
            $coun = data.count;
            $dat = data.data;
            var select=document.getElementById("selectOne");
            for ($i = 0; $i < $coun; $i++) {
                $product = $dat[$i].user;
                $email=$dat[$i].email;
                select.innerHTML += "<option value='"+$product+"' >"+$product+"</option>";
            }
            layui.form.render('select');

        },
        error: function (data) {
            layer.msg("查询出错");
        }
    });


    $.ajax({
        type: 'post',
        cache: false,
        url: '../php/admin/select_task.php',
        data: {
          "user":$user,
        },
        dataType: 'json',
        success: function (data) {
            $coun = data.count;
            $dat = data.data;

            for ($i = 0; $i < $coun; $i++) {
                $id = $dat[$i].id;
                $user = $dat[$i].task_admin;
                $title=$dat[$i].title;
                $time=$dat[$i].time_over;
                $task_a=$dat[$i].task_a;
                $user_a=$dat[$i].user_a;
                $remark=$dat[$i].remark;
                $content=$dat[$i].task_content;
                $addtime=$dat[$i].task_time;
                $complete=$dat[$i].complete;
                $url=$dat[$i].url;
                var li_id = $id;
                $li = $("<div id="+$id+" class=\"layui-card\" id=\"card\">\n" +"<hr class=\"layui-bg-red\">"+
                    "<div class=\"layui-card-header\">"+"任务详情："+"<button style=\"float: right;margin-top: 3px\" class=\"layui-btn layui-btn-primary layui-btn-sm\" onclick='del("+$id+",\""+$url+"\")'><i class=\"layui-icon\"></i></button>"+"<button id='file' class=\"layui-btn layui-btn-xs\" >"+"<a href='"+$url+"' download='file' style='color: white'>"+"附件下载"+"</a>"+"</button>"+"<a id='overTime'>"+"任务截至时间："+$time+"</a>"+"<a id='overTime'>"+"完成时间："+$complete+"</a>"+"</div>" +
                    "                    <div class=\"layui-card-body\">\n" +$content+"<br>" +
                   "<hr>"+"<div class=\"layui-card-header\">"+"任务："+"<a id='overTime'>"+"接受任务的成员："+$user_a+"</a>"+"</div>"+
                    $task_a+
                    "<hr>"+"<div class=\"layui-card-header\">"+"备注："+"</div>"+
                    $remark+"<br>"+
                    "                       <span id=\"time\">"+ "发布时间："+$addtime+"&nbsp;|&nbsp;"+"发布人："+$user+"</span>" +
                    "                          <br>\n" +
                    "                    </div>\n" +
                    "                </div>");
                $("#uul").append($li);

            }
        },
        error: function (data) {
            layer.msg("暂无您发布的任务");
        }
    });
    function del($id,url) {
        layer.confirm('真的删除行么', function(index) {
            $.ajax({
                type:'post',
                cache:'cache',
                url:'../php/admin/del_task.php',
                data:{
                    "id":$id,
                    "url":url,
                },
                dataType:'json',
                success:function (dat) {
                    layer.msg(dat.message);
                    var li=document.getElementById($id);
                    li.remove();
                },
                error:function (dat) {
                    layer.msg("删除失败")
                },

            });
        });





    }

    function fileDownload($url) {

        var fileD=document.getElementById("fileD");
        fileD.setAttribute("href",$url);
        fileD.setAttribute("download","file");
    }
    function out() {
        layer.confirm('真的要退出吗？', function(index) {
            window.location.href="../php/user/clearSession.php";
        })
    }
</script>

</body>
</html>